
<template>
  <div>
    <el-dialog
      v-el-drag-dialog
      :title="'销售人员列表'"
      :visible.sync="visible"
      :modal="false"
      width="1024px"
    >
      <el-table-form ref="dataForm" class="form" :model="query" label-position="right" label-width="80px">
        <el-form-item label="生效机构" prop="orgSNNo">
          <el-tree-select
            v-model="query.orgSNNo"
            :clearable="false"
            class="tree-select"
            :popover-width="360"
            :select-params="{
              clearable: true,
              placeholder: '请选择组织机构',
              loading: $loadingPlugin.getTreeData
            }"
            :tree-params=" {
              'expand-on-click-node': false,
              'highlight-current': true,
              props:{
                value:'orgSNNo',
                label: 'orgName',
                disabled: 'disabled',
              },
              data: orgTreeData
            }"
            @node-click="selectOrgTreeChange"
          />
        </el-form-item>
        <el-form-item label="人员名称" class="merge-item">
          <el-input v-model="query.userName" clearable placeholder="请输入" />
        </el-form-item>
        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </template>
      </el-table-form>
      <lb-table
        ref="lbTable"
        max-height="400"
        border
        highlight-current-row
        element-loading-text="加载中"
        :column="tableData.column"
        :data="tableData.data"
        pagination
        :total="tableData.total"
        @fetch="fetchData"
        @row-dblclick="dbclick"
        @current-change="handleCurrentChange"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">
          关闭
        </el-button>
        <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { searchInsureList } from '@/api'
  export default {
    name: 'insure',
    props: ['orgTreeData', 'userType'],
    data() {
      return {
        visible: false,
        userTypeData: [{ 'value': 1, 'desc': '超级管理员', 'key': 'UT_ADMIN' }, { 'value': 2, 'desc': '管理员', 'key': 'UT_GLY' }, { 'value': 4, 'desc': '主管', 'key': 'UT_MG' }, { 'value': 8, 'desc': '话务员', 'key': 'MSG_HWY' }, { 'value': 16, 'desc': '建档工', 'key': 'MSG_JDG' }, { 'value': 32, 'desc': '配送员', 'key': 'UT_PSY' }, { 'value': 64, 'desc': '销售员', 'key': 'UT_XS' }, { 'value': 256, 'desc': '司机', 'key': 'UT_SJ' }, { 'value': 512, 'desc': '装卸工', 'key': 'UT_ZXG' }, { 'value': 128, 'desc': '其它', 'key': 'UT_QT' }, { 'value': 1024, 'desc': '安检负责人', 'key': 'UT_AJ_MG' }, { 'value': 2048, 'desc': '安检员', 'key': 'UT_AJ_USER' }, { 'value': 4096, 'desc': '充装工', 'key': 'UT_CZG' }],
        selectRow: {},
        query: {
          userName: null,
          'utype': 64
        },
        tableData: {
          column: [
            { label: '编码', prop: 'userCode', width: 80, align: 'center' },
            { label: '名称', prop: 'userName', width: 100, align: 'center' },
            /* {label:'性别',prop:'usex',width:100,align:'left',formatter:this.formatter.userSexFormat}, */
            { label: '类型', prop: 'utype', width: 150,
              render: (h, { row, $index, cloumn }) => <div >{this.getUserType(row, cloumn)}</div>
            },
            { label: '电话', prop: 'uphone', width: 100, align: 'center' },
            { label: '所属机构', prop: 'orgName', width: 120, align: 'center' },
            { label: '地址', prop: 'uaddress', width: '' }
          ],
          data: []
        }
      }
    },
    created() {

    },
    methods: {
      open() {
        this.visible = true
      },
      async fetchData({ currentPage = 1, pageSize = 20 }) {
        const query = {
          ...this.query,
          currentPage,
          pageSize
        }
        const res = await searchInsureList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      dbclick(row) {
        console.log(row)
        this.$emit('changeSaleMan', { name: row.userName, id: row.userId })
        this.visible = false
      },
      selectOrgTreeChange(val) {

      },
      search() {
        // userName":"11","orgSNNo":"001000000000000000000","utype":64
        this.fetchData({})
      },
      getUserType(row, cloumn) {
        var utdata = this.userTypeData
        var val = row['utype']
        var txt = ''
        for (var key in utdata) {
          var value = utdata[key]
          if ((key & val) > 0) {
            txt += value.desc + ','
          }
        }
        return txt
      },
      handleCurrentChange(val) {
        console.log(val)
        this.selectRow = val
      },
      success() {
        if (this.selectRow.userId) {
          var row = this.selectRow
          this.$emit('changeSaleMan', { name: row.userName, id: row.userId })
        } else {
          this.$message.warning('请选择销售人员')
          return ''
        }
        this.visible = false
      }
    }
  }
</script>

<style lang="scss" scope>
  .iconfont {
    font-size: 16px;
  }
  .fail {
    color: #a9a9a9;
  }
</style>

